package GUILayer;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import CtrLayer.ProductCtr;
import ModelLayer.*;
import CtrLayer.SalesOrderCtr;

public class PartOrderTableModel  extends AbstractTableModel {
	private SalesOrderCtr soCtr; 
	private ArrayList<PartOrder> li = new ArrayList<PartOrder>();
	private String[] columnNames = {"Products in basket", "Country of origin", "Amount", "Sales Price", "Price"};
	
	public PartOrderTableModel(){
		new ProductCtr();
		soCtr = SalesOrderCtr.getInstance();
	}
	public void populateList()
	{
		ArrayList<PartOrder> tmpList = soCtr.listPartOrders();
		System.out.println("temporary list size: " + tmpList.size());
		if(tmpList != null)
		{
			this.li = tmpList;
		}
		fireTableDataChanged();
	}

	@Override
	public String getColumnName(int columnIndex){
		return columnNames[columnIndex];
	}

	@Override     
	public int getRowCount() {
		return li.size();
	}

	@Override        
	public int getColumnCount() {
		return 5;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		PartOrder p = li.get(rowIndex);
		switch (columnIndex) {
		
		case 0: 
			return p.getP().getName();
		case 1:
			return p.getP().getCountryOfOrigin();
		case 2:
			return p.getAmount();
		case 3:
			return p.getP().getSalesPrice();
		case 4:
			return p.getAmount() * p.getP().getSalesPrice();
				
		}
		return null;
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex){
		switch (columnIndex){
		case 0:
			return String.class;
		case 1:
			return String.class;
		case 2:
			return int.class;
		case 3:
			return Double.class;
		case 4:
			return Double.class;
		}
		return null;
	}
}